package com.missilike.api.controller;


import com.alibaba.fastjson.JSON;
import com.missilike.biz.IMilFollowRelationshipService;
import com.missilike.biz.IMilFriendApplyService;
import com.missilike.biz.IMilFriendRelationshipService;
import com.missilike.core.domain.DeleteFriendReuqest;
import com.missilike.core.result.BaseResultCode;
import com.missilike.core.result.BizResultCode;
import com.missilike.core.result.CommonResult;
import com.missilike.core.utils.ResultUtils;
import com.missilike.domain.response.MilAccountInfo;
import com.missilike.repository.entity.MilAccount;
import com.missilike.repository.entity.MilFriendApply;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author pengpengli
 * @since 2018-12-19
 */
@Slf4j
@RestController
@RequestMapping("/api/friend")
public class FriendController {
    private final IMilFriendRelationshipService friendRelationshipService;
    private final IMilFriendApplyService friendApplyService;
    private final IMilFollowRelationshipService followRelationshipService;

    @Autowired
    public FriendController(IMilFriendRelationshipService friendRelationshipService, IMilFriendApplyService friendApplyService, IMilFollowRelationshipService followRelationshipService) {
        this.friendRelationshipService = friendRelationshipService;
        this.friendApplyService = friendApplyService;
        this.followRelationshipService = followRelationshipService;
    }

    @ApiOperation(value = "getFriends", notes = "获取用户好友列表")
    @RequestMapping(value = "/getFriends", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult getFriends(@RequestParam String userAccountId) {
        List<MilAccountInfo> accounts = friendRelationshipService.getAllFriends(userAccountId);
        if (null == accounts) {
            return ResultUtils.error(BaseResultCode.INVALID_PARAMETER.getCode(), String.format(BaseResultCode.INVALID_PARAMETER.getMsg(), "用户ID"));
        }
        return ResultUtils.ok(accounts);
    }


    @ApiOperation(value = "apply", notes = "好友申请")
    @RequestMapping(value = "/apply", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult apply(@RequestParam String fromAccountId, @RequestParam String toAccountId) {
        if (null == fromAccountId || null == toAccountId) {
            return ResultUtils.error(BaseResultCode.PARAMETER_ERROR);
        }
        MilFriendApply apply = new MilFriendApply();
        apply.setFromAccountId(fromAccountId);
        apply.setToAccountId(toAccountId);
        if (friendRelationshipService.isFriend(fromAccountId, toAccountId)) {
            return ResultUtils.error(BizResultCode.BLACKLIST_RELATIONSHIP);
        }
        friendApplyService.friendRequest(apply);
        return ResultUtils.ok();
    }

    @ApiOperation(value = "applyResponse", notes = "申请回复/同意或拒绝.applyStatus:1-同意，2-拒绝")
    @RequestMapping(value = "/applyResponse", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult applyResponse(@RequestParam Long applyId, @RequestParam Integer applyStatus) {
        if (null == applyId) {
            return ResultUtils.error(BaseResultCode.PARAMETER_ERROR);
        }

        MilFriendApply apply = new MilFriendApply();
        apply.setId(applyId);
        apply.setApplyStatus(applyStatus);
        friendApplyService.friendRequestResponse(apply);
        return ResultUtils.ok();
    }

    @ApiOperation(value = "addToBlackList", notes = "添加黑名单")
    @RequestMapping(value = "/addToBlackList", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult addToBlackList(String fromAccountId, @RequestParam String toAccountId) {
        if (StringUtils.isBlank(fromAccountId) || StringUtils.isBlank(toAccountId)) {
            return ResultUtils.error();
        }
        friendRelationshipService.addToBlackList(fromAccountId, toAccountId);
        return ResultUtils.ok();
    }


    @ApiOperation(value = "getApply", notes = "获取好友申请列表(applyStatus：0-发起,1-同意，2-拒绝)")
    @RequestMapping(value = "/getApply", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult getApply(@RequestParam String accountId, @RequestParam(defaultValue = "0") Long lastId, @RequestParam(defaultValue = "20") Integer pageSize) {

        if (StringUtils.isBlank(accountId)) {
            return ResultUtils.error();
        }
        return ResultUtils.ok(friendApplyService.getApply(accountId, lastId, pageSize));
    }

    @ApiOperation(value = "followUser", notes = "关注用户")
    @RequestMapping(value = "/followUser", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult followUser(@RequestParam String accountId, @RequestParam String followAccountId) {
        if (StringUtils.isBlank(accountId) || StringUtils.isBlank(followAccountId)) {
            return ResultUtils.error(99901, "参数不能为空");
        }
        return followRelationshipService.InsertRelationship(accountId, followAccountId);
    }


    @ApiOperation(value = "getFollowUser", notes = "获取用户关注的用户")
    @RequestMapping(value = "/getFollowUser", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult getFollowUser(@RequestParam String accountId) {

        if (StringUtils.isBlank(accountId)) {
            return ResultUtils.error(99901, "参数不能为空");
        }
        return ResultUtils.ok(followRelationshipService.getFollowuser(accountId));
    }

    @ApiOperation(value = "getBeFollowed", notes = "获取用户被关注的用户列表")
    @RequestMapping(value = "/getBeFollowed", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult getBeFollowed(@RequestParam String accountId) {

        if (StringUtils.isBlank(accountId)) {
            return ResultUtils.error(99901, "参数不能为空");
        }
        return ResultUtils.ok(followRelationshipService.watchingUsers(accountId));
    }

    @ApiOperation(value = "getBeFollowedCount", notes = "获取用户被关注的用户数量")
    @RequestMapping(value = "/getBeFollowedCount", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult getBeFollowedCount(@RequestParam String accountId) {

        if (StringUtils.isBlank(accountId)) {
            return ResultUtils.error(99901, "参数不能为空");
        }
        return ResultUtils.ok(followRelationshipService.getBeFollowedCount(accountId));
    }

    @ApiOperation(value = "unfollowUser", notes = "取消关注用户")
    @RequestMapping(value = "/unfollowUser", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult unfollowUser(@RequestParam String accountId, @RequestParam String followAccountId) {
        if (StringUtils.isBlank(accountId) || StringUtils.isBlank(followAccountId)) {
            return ResultUtils.error(99901, "参数不能为空");
        }
        return followRelationshipService.deleteRelationship(accountId, followAccountId);
    }

    @ApiOperation(value = "deleteFriend", notes = "删除好友")
    @RequestMapping(value = "/deleteFriend", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public CommonResult deleteFriend(@Valid @RequestBody DeleteFriendReuqest reuqest) {
        log.info("Start to execute /access/deleteFriend body request={}", JSON.toJSON(reuqest));
        friendRelationshipService.deleteFriend(reuqest);
        log.info("Completed execute /access/deleteFriend responese={}", JSON.toJSON(reuqest));
        return ResultUtils.ok();
    }

}

